package org.lex.swing.widgets;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.lex.swing.Swings;


public class ProgressDialog extends JDialog {
	private JPanel pane = null;
	private JLabel promptLabel = null;
	private JLabel progressLabel = null;
	private JProgressBar progressBar = null;
	private JPanel gluePane = null;
	private JButton closeButton = null;

	public static void main(String[] args) {
		Swings.show(new ProgressDialog(null));
	}

	public ProgressDialog(Window owner) {
		super(owner);
		initialize();
		this.setLocationRelativeTo(owner);
	}

	public void setPrompt(String prompt) {
		this.promptLabel.setText(prompt);
	}

	public void setProgress(String msg) {
		this.progressLabel.setText(msg);
	}

	public void done() {
		this.getProgressBar().setIndeterminate(false);
		this.getProgressBar().setValue(this.getProgressBar().getMaximum());
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.getCloseButton().setEnabled(true);
	}

	public JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("Close");
			closeButton.setEnabled(false);
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return closeButton;
	}

	public JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setIndeterminate(true);
		}
		return progressBar;
	}

	private void initialize() {
		this.setSize(300, 200);
		this.setModal(true);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.setContentPane(getPane());
	}

	private JPanel getPane() {
		if (pane == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 4;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.weightx = 10.0;
			gridBagConstraints3.weighty = 10.0;
			gridBagConstraints3.gridy = 3;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.insets = new Insets(0, 0, 12, 0);
			gridBagConstraints2.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.insets = new Insets(0, 0, 6, 0);
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridy = 1;
			progressLabel = new JLabel();
			progressLabel.setText("Message");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.anchor = GridBagConstraints.CENTER;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new Insets(0, 0, 20, 0);
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.gridy = 0;
			promptLabel = new JLabel();
			promptLabel.setText("Prompt");
			pane = new JPanel();
			pane.setLayout(new GridBagLayout());
			pane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
			pane.add(promptLabel, gridBagConstraints);
			pane.add(progressLabel, gridBagConstraints1);
			pane.add(getProgressBar(), gridBagConstraints2);
			pane.add(getGluePane(), gridBagConstraints3);
			pane.add(getCloseButton(), gridBagConstraints11);
		}
		return pane;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
			gluePane.setLayout(new GridBagLayout());
		}
		return gluePane;
	}
}
